//
//  KABWebViewVC.m
//  kabu
//
//  Created by XiangDongMBP on 2017/4/22.
//  Copyright © 2017年 East. All rights reserved.
//

#import "KABWebViewVC.h"
#import <WebKit/WebKit.h>
#import "YHWebViewProgressView.h"

@interface KABWebViewVC ()<WKNavigationDelegate>
@property (nonatomic, strong) WKWebView *webView;
@property (nonatomic, strong) YHWebViewProgressView *progressView;
@end

@implementation KABWebViewVC

#pragma mark - life cycle
- (void)dealloc {
    [self.webView removeObserver:self.progressView forKeyPath:@"estimatedProgress"];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initNavigationBar];
    [self initSubViews];
}

- (BOOL)hidesBottomBarWhenPushed {
    return YES;
}

- (void)initNavigationBar {
    self.navigationItem.title = @"加载中...";
}

- (void)initSubViews {
    [self.view addSubview:self.webView];
    
    // 创建进度条
    YHWebViewProgressView *progressView = [[YHWebViewProgressView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.navigationController.navigationBar.frame), CGRectGetWidth(self.view.bounds), 2)];
    progressView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleBottomMargin;
    // 指定WKWebView对象来监听进度
    [progressView useWkWebView:self.webView];
    // 添加到视图
    [self.view addSubview:progressView];
    self.progressView = progressView;
    
    // 请求连接
    [self.webView loadRequest:[NSURLRequest requestWithURL:self.requestUrl]];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - WKNavigationDelegate
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    self.navigationItem.title = webView.title;
}

#pragma mark - event response
#pragma mark - private methods
#pragma mark - network request
#pragma mark - getters and setters
- (WKWebView *)webView {
    if (!_webView) {
        _webView = [[WKWebView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        _webView.navigationDelegate = self;
    }
    return _webView;
}

@end
